Docker CLI Commands - WittyWriter

Docker CLI Commands

📘 Key Concepts and Definitions

🧮 Command Structure

Docker commands follow a consistent structure of management commands and sub-commands.

docker [management_command] [sub_command] [OPTIONS] [ARGUMENTS]

Example: docker container run -d -p 8080:80 --name webserver nginx

While older syntax like docker run still works, the newer, more explicit syntax like docker container run is recommended for clarity.

🛠️ Core Commands Reference

Image Management

CommandDescription
docker image build -t [name:tag] .Builds an image from a Dockerfile in the current directory.
docker image pull [name:tag]Pulls an image from a registry (like Docker Hub).
docker image push [name:tag]Pushes an image to a registry.
docker image lsLists all local images.
docker image rm [image_id]Removes one or more images.

Container Management

CommandDescription
docker container run [image]Creates and starts a new container from an image.
docker container start / stop [container_id]Starts/stops an existing container.
docker container lsLists running containers (use -a to see all containers).
docker container logs [container_id]Fetches the logs of a container. (Use -f to follow).
docker container exec -it [id] [cmd]Executes a command inside a running container (e.g., bash for a shell).
docker container rm [container_id]Removes one or more containers. (Use -f to force).

System & Data Management

CommandDescription
docker volume create [name]Creates a named volume.
docker volume lsLists all volumes.
docker network lsLists all networks.
docker system prune -aRemoves all unused data (containers, networks, images). Use with caution!

🧭 Basic Development Workflow

  1. Write a Dockerfile that defines your application environment.
  2. Build the image: Run docker image build -t my-app . to create a local image tagged as "my-app".
  3. Run the container: Run docker container run -d -p 5000:5000 --name my-app-instance my-app to start your app.
  4. Check logs for errors: Use docker container logs my-app-instance to see the output from your app.
  5. Debug inside the container: If needed, get a shell inside the running container with docker exec -it my-app-instance bash.
  6. Stop and remove: Once finished, stop with docker container stop my-app-instance and remove with docker container rm my-app-instance.

⌨️ Productivity Tips

📊 Common docker run Flags

FlagNameDescription
-d--detachRuns the container in the background (detached mode).
-p [host]:[container]--publishMaps a port on the host to a port in the container (e.g., -p 8080:80).
-v [host]:[container]--volumeMounts a host directory or a named volume into the container.
-e [KEY=VALUE]--envSets an environment variable inside the container.
--name [name]Assigns a custom name to the container for easy reference.
--rmAutomatically removes the container when it exits.
-it--interactive --ttyProvides an interactive terminal session (used with exec).

🧪 Example: Simple Node.js Web App

Dockerfile

# 1. Use an official Node.js runtime as a base image
FROM node:18-slim

# 2. Set the working directory in the container
WORKDIR /app

# 3. Copy package files and install dependencies
COPY package*.json ./
RUN npm install

# 4. Copy the rest of the application source code
COPY . .

# 5. Expose the port the app runs on
EXPOSE 3000

# 6. Define the command to run the app
CMD [ "node", "server.js" ]

Build and Run Commands

# Build the image
docker build -t my-node-app .

# Run the container
docker run -d -p 3000:3000 --name webapp my-node-app

🧹 Troubleshooting Common Issues

📚 References and Further Reading

🍪 We use cookies to improve your experience. Learn more